<template>
    <addEditBaseWxInform ref="addEditBaseWxInform" />
    <addFileView ref="addFileView" />
    <div class="app-table">
        <div class="app-body">
            <el-form :inline="true" class="demo-form-inline" v-model="form">
                <el-form-item label="姓名">
                    <el-input placeholder="请输入姓名" v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="工号">
                    <el-input placeholder="请输入工号" v-model="form.studentNumber"></el-input>
                </el-form-item>
                <el-form-item label="openid">
                    <el-input placeholder="请输入openid" v-model="form.openid"></el-input>
                </el-form-item>

                <el-form-item label="昵称">
                    <el-input placeholder="请输入昵称" v-model="form.nickName"></el-input>
                </el-form-item>
                <el-form-item label="类型">
                    <el-select v-model="form.userType" clearable placeholder="请选择类型" style="width: 192px;">
                        <el-option v-for="item in userTypes" :key="item.value" :label="item.label"
                            :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="校区">
                    <el-select v-model="form.campus" clearable placeholder="请选择校区" style="width: 192px;">
                        <el-option v-for="item in campusData" :key="item.value" :label="item.label"
                            :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="登录状态">
                    <el-select v-model="form.isLogin" clearable placeholder="请选择校区" style="width: 192px;">
                        <el-option label="已登录" :value="1" />
                        <el-option label="未登录" :value="2" />
                    </el-select>
                </el-form-item>

                <el-form-item>
                    <el-button class="serach_btn" type="primary" @click="onSubmit" plain
                        @keyup.enter="searchEnter">搜索</el-button>
                    <el-button class="export_btn" type="warning" @click="exportExcel" plain>导出在职人员</el-button>
                </el-form-item>
            </el-form>


            <el-table class="bable_box" size="default" :data="list" style="width: 100%"
                :row-class-name="tableRowClassName">
                <el-table-column prop="id" label="id" width="90" />
                <el-table-column prop="name" label="头像" width="100">
                    <template #default="scope">
                        <el-avatar shape="square" :size="50" :fit="fit" :src="scope.row.avatarUrl" />
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="姓名" width="100" />
                <el-table-column prop="studentNumber" label="学号/工号" width="150" />
                <!-- <el-table-column prop="province" label="地址" />
                <el-table-column prop="country" label="国家" /> -->
                <el-table-column prop="openid" label="openid" width="350px" />
                <el-table-column prop="nickName" label="昵称" />
                <el-table-column label="身份">
                    <template #default="scope">
                        <p v-if="scope.row.userType == 1">学生</p>
                        <p v-if="scope.row.userType == 2">人员</p>
                    </template>
                </el-table-column>
                <el-table-column label="校区">
                    <template #default="scope">
                        <p v-if="scope.row.campus == 1">{{ campusName }}</p>
                        <p v-if="scope.row.campus == 2">{{ campusName1 }}</p>
                    </template>
                </el-table-column>
                <el-table-column label="登录状态">
                    <template #default="scope">
                        <p v-if="scope.row.isLogin == 1">已登录</p>
                        <p v-if="scope.row.isLogin == 0" style="color: #F41515;">未登录</p>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="200px">
                    <template #default="scope">
                        <el-button class="edit_btn1" size="small" type="success"
                            @click="loginBaseTeacherCar(scope.row.id)">登录
                        </el-button>
                        <el-button class="edit_btn" size="small" type="success"
                            @click="editBaseTeacherCar(scope.row)">编辑
                        </el-button>
                        <el-button class="del_btn" size="small" type="success"
                            @click="delBaseTeacherCar(scope.row.id)">删除
                        </el-button>
                    </template>
                </el-table-column>
                <template v-slot:empty>
                    <el-empty description="暂无数据"></el-empty>
                </template>
            </el-table>

            <el-config-provider :locale="locale">
                <el-pagination :total="total" :page-size="limit" :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
                    layout="total, sizes, prev, pager, next, jumper" @size-change="changePageSize"
                    @current-change="changeCurrentPage">
                </el-pagination>
            </el-config-provider>
        </div>
        <bottomView></bottomView>

    </div>
</template>
<script>
import addEditBaseWxInform from "@/components/baseInfo/addEditBaseWxInform.vue";
import addFileView from "@/components/addExcelFile.vue";
import { ElConfigProvider, ElMessageBox, ElMessage, ElLoading } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { httpService } from "@/utils/httpService";
import { Picture as IconPicture, Upload } from "@element-plus/icons-vue";
import bottomView from "@/components/bottom.vue";
import { API_BASE_NAME, API_BASE_NAME1, API_BASE_NAME2, API_BASE_NAME3 } from '../../constants';
export default {
    name: "BaseTeacherCarListView",
    data() {
        return {
            campusName: API_BASE_NAME2,
            campusName1: API_BASE_NAME3,
            type: 0,
            title: "",
            page: 0,
            pageSize: 10,
            total: 0,
            limit: 10,
            list: [],
            form: {
                name: '',
                nickName: '',
                studentNumber: '',
                userType: '',
                campus: '',
                openid: '',
                isLogin: '',
            },
            userTypes: [
                {
                    value: 1,
                    label: '学生',
                },
                {
                    value: 2,
                    label: '人员',
                },

            ],
            campusData: [
                {
                    value: 1,
                    label: API_BASE_NAME,
                },
                {
                    value: 2,
                    label: API_BASE_NAME1,
                },
            ],
        };
    },
    components: {
        addEditBaseWxInform,
        addFileView, bottomView,
        IconPicture,
        ElConfigProvider,
        ElMessageBox,
        ElMessage, Upload
    },
    setup() {
        return {
            locale: zhCn,
        };
    },
    mounted() {
        this.getBaseWxInformList()
    },
    watch: {
        // $route() {
        //     this.getBaseWxInformList()
        // },
    },
    methods: {
        searchEnter: function (e) {
            // 使用 which 和 keyCode 属性来解决兼容问题
            var keyCode = window.event ? e.keyCode : e.which;
            var val = e.target.value;
            if (keyCode == 13 && val) {
                this.getBaseWxInformList();
            }
        },
        editBaseTeacherCar(obj) {
            console.log(obj.id, obj);
            this.$refs.addEditBaseWxInform.title = "修改信息"
            // this.$refs.addEditBaseWxInform.getBaseWxInformById(obj.id, obj.studentNumbe)
            this.$refs.addEditBaseWxInform.addBaseWxInformForm.studentNumber = obj.studentNumber;
            this.$refs.addEditBaseWxInform.addBaseWxInformForm.id = obj.id;
            this.$refs.addEditBaseWxInform.addBaseWxInformForm.name = obj.name;
            this.$refs.addEditBaseWxInform.addBaseWxInformForm.userType = obj.userType;
            this.$refs.addEditBaseWxInform.dialogFormVisible = true;
        },
        loginBaseTeacherCar(id) {
            ElMessageBox.confirm("确定要登录吗？", "提醒", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    let params = new FormData()
                    params.append('id', id)
                    params.append('isLogin', 1)
                    httpService("/javaApi/registerInfo/edit", params, "post").then(
                        (data) => {
                            if (data.code == 200) {
                                ElMessage.success("登录成功");
                                this.getBaseWxInformList();
                            } else {
                                ElMessage.error("登录失败");
                            }
                        }
                    );
                })
                .catch(() => {
                });
        },
        delBaseTeacherCar(id) {
            ElMessageBox.confirm("确定要删除本条记录吗？", "提醒", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    httpService("/javaApi/registerInfo/deleteById/", "id=" + id, "post").then(
                        (data) => {
                            if (data.code == 200) {
                                ElMessage.success("删除成功");
                                this.getBaseWxInformList();
                            } else {
                                ElMessage.error(data.message);
                            }
                        }
                    );
                })
                .catch(() => {
                });
        },
        onSubmit() {
            this.getBaseWxInformList();
        },
        // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
        changePageSize(size) {
            this.pageSize = size;
            this.limit = size;
            this.getBaseWxInformList();
        },

        // 改变页码，page：回调参数，表示当前选中的“页码”
        changeCurrentPage(page) {
            console.log('page', page);
            this.page = page;
            this.getBaseWxInformList();
        },
        tableRowClassName({ row, rowIndex }) {
            return "";
        },
        getBaseWxInformList() {
            console.log(this.form.isLogin);
            httpService(
                // "/javaApi/registerInfo/list/1/10",
                "/javaApi/registerInfo/list/" + this.page + "/" + this.limit,
                {
                    'name': this.form.name,
                    'nickName': this.form.nickName,
                    'studentNumber': this.form.studentNumber,
                    'userType': this.form.userType ? this.form.userType : this.form.userType = "",
                    'campus': this.form.campus ? this.form.campus : this.form.campus = "",
                    'openid': this.form.openid,
                    'isLogin': !this.form.isLogin ? "" : this.form.isLogin == 2 ? 0 : this.form.isLogin,
                },
                "get"
            ).then((data) => {
                console.log(data);
                this.total = data.total
                this.list = data.list
            });
        },
        exportExcel() {
            var loadingInstance = ElLoading.service({
                lock: true, text: '加载中', background: 'rgba(0, 0, 0, 0.7)',
            });
            httpService(
                "/javaApi/registerInfo/exportExcelTeacher",
                {
                    'name': this.form.name,
                    'nickName': this.form.nickName,
                    'studentNumber': this.form.studentNumber,
                    'userType': this.form.userType ? this.form.userType : this.form.userType = "",
                    'campus': this.form.campus ? this.form.campus : this.form.campus = "",
                },
                "get"
            ).then((data) => {
                loadingInstance.close()
                if (data.code == 200) {
                    window.open(`https://wx.zhengzhong.cn${data.message}`, '_blank')
                    // window.location.href=`https://wx.zhengzhong.cn${data.message}`
                }
            });
        },
    },
};
</script>
<style scoped>
.el-main {
    padding: 0px;
}

.teacher-content {
    width: 100%;
}

.app-table {
    background: #eff3f5;
    padding: 5px 10px 10px;
    /* width: 100%; */
}


.app-separator {
    padding-top: 10px;
    padding-bottom: 20px;
    padding-left: 10px;
}

.app-body {
    background: white;
    padding: 20px 20px 10px 20px;
    margin-bottom: 10px;
    min-height: calc(100vh - 204px);
}

.el-table .warning-row {
    --el-table-tr-bg-color: var(--el-color-warning-lighter);
}

.el-table .success-row {
    --el-table-tr-bg-color: var(--el-color-success-lighter);
}

.el-pagination {
    text-align: center;
    margin-top: 10px;
}

.bable_box p {
    margin: 0;
}

.edit_btn1 {
    background-color: transparent;
    /* color: #007EFF; */
    color: #007EFF;
    border: none;
}

.edit_btn {
    background-color: transparent;
    /* color: #007EFF; */
    color: #00c78c;
    border: none;
}

.serach_btn {
    background-color: #007EFF;
    color: #fff;
    border-radius: 8px;
    border: none;
}

.export_btn {
    background-color: #FF7200;
    border-radius: 8px;
    color: #fff;
    border: none;
}

.del_btn {
    background-color: transparent;
    color: #FF0000;
    border: none;
}
</style>
